package com.wego.controller;

import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.entity.domain.OrderItem;
import com.wego.entity.query.OrderItemQuery;
import com.wego.entity.vo.OrderItemVO;
import com.wego.service.GoodsService;
import com.wego.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * (tb_order_item)表控制层
 *
 * @author Li Chuanwei
 */
@Controller
@RequestMapping("/manager/order_item")
public class OrderItemController {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private GoodsService goodsService;
    
    @GetMapping("/list")
    public @ResponseBody TableBean list(OrderItemQuery orderItemQuery) {
        final PageBean<OrderItem> orderItemPageBean = orderItemService.selectPage(orderItemQuery);
        PageBean<OrderItemVO> pageBean = orderItemPageBean2OrderItemVOPageBean(orderItemPageBean);
        List<OrderItemVO> orderItemVOS = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean tableBean = new TableBean<OrderItemVO>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(orderItemVOS);
        tableBean.setCount(count);
        return tableBean;
    }

    private PageBean<OrderItemVO> orderItemPageBean2OrderItemVOPageBean(PageBean<OrderItem> pageBean) {
        PageBean<OrderItemVO> orderItemVOPageBean = new PageBean<>();
        orderItemVOPageBean.setPageNum(pageBean.getPageNum());
        orderItemVOPageBean.setPageSize(pageBean.getPageSize());
        orderItemVOPageBean.setPages(pageBean.getPages());
        orderItemVOPageBean.setTotal(pageBean.getTotal());

        List<OrderItemVO> list = new ArrayList<>();
        for (OrderItem orderItem : pageBean.getResult()) {
            final OrderItemVO orderItemVO = orderItem2OrderItemVO(orderItem);
            list.add(orderItemVO);
        }
        orderItemVOPageBean.setResult(list);

        return orderItemVOPageBean;
    }

    private OrderItemVO orderItem2OrderItemVO(OrderItem orderItem) {
        if (orderItem == null) {
            return null;
        }
        OrderItemVO orderItemVO = new OrderItemVO();
        orderItemVO.setId(orderItem.getId());
        orderItemVO.setAmount(orderItem.getAmount());
        orderItemVO.setPrice(orderItem.getMoney().divide(new BigDecimal(orderItem.getAmount()), 2, BigDecimal.ROUND_HALF_UP));
        orderItemVO.setMoney(orderItem.getMoney());
        orderItemVO.setGoodsId(orderItem.getGoodsId());
        orderItemVO.setGoodsName(goodsService.selectByPrimaryKey(orderItemVO.getGoodsId()).getName());
        orderItemVO.setOrderId(orderItem.getOrderId());
        return orderItemVO;
    }

    @GetMapping("/details")
    public String map(String orderId, Model model){
        model.addAttribute("orderId",orderId);
        return "manager/orderItem_details";
    }

}
